<?php

namespace addons\cardocr\library\traits;
use addons\cardocr\library\Card;

trait Check
{
    /**
     * 检测重复身份证id
     *
     * @param string $username 身份证id
     * @param string $id 排除身份证id
     */
    public function checkidcardavailable()
    {
        $idnum = $this->request->request('idnum');
        if (!$idnum) {
            $params = $this->request->post("row/a");
            $idnum = $params['idnum'];
        }

        // $id = (int)$this->request->request('id');
        $count = \addons\cardocr\model\Cardocr::where('idnum', '=', $idnum)->count();
        if ($count > 0) {
            $this->error(__('身份证ID已经被占用'));
        }
        $this->success();
    }

    /**
     * 检测重复用户id
     *
     * @param string  用户$user_id
     */
    public function checkuseridavailable()
    {
        $user_id = (int)$this->request->request('user_id');
        if (!$user_id) {
            $params = $this->request->post("row/a");
            $user_id = $params['user_id'];
        }

        $count = \addons\cardocr\model\Cardocr::where('user_id', '=', $user_id)->count();
        if ($count > 0) {
            $this->error(__('用户记录已存在'));
        }
        $this->success();
    }

    /**
     * @param $ImageUrl 身份证正面url
     * @return array|mixed
     */
    public function checkfront($ImageUrl)
    {
        $ImageBase64 = Card::base64EncodeImage($ImageUrl);
        if (is_array($ImageBase64)) {
            $this->error($ImageBase64['msg']);
        }

        $data['ImageBase64'] = $ImageBase64;
        $data['CardSide'] = 'FRONT';


        $orc_frontdata = Card::checkcardocr(json_encode($data), $data['CardSide']);
        if (!$orc_frontdata || !empty($orc_frontdata['code'])) {
            $this->error($orc_frontdata['msg']);
        }

        return $orc_frontdata;
    }

    /**
     * @param $ImagebackUrl 身份证反面url
     * @return array
     */
    public function checkback($ImagebackUrl)
    {
        $ImageBase64 = Card::base64EncodeImage($ImagebackUrl);
        if (is_array($ImageBase64)) {
            $this->error($ImageBase64['msg']);
        }

        $data['ImageBase64'] = $ImageBase64;
        $data['CardSide'] = 'BACK';


        $orc_backdata = Card::checkcardocr(json_encode($data), $data['CardSide']);
        if (!$orc_backdata || !empty($orc_backdata['code'])) {
            $this->error($orc_backdata['msg']);
        }

        $validdatestart = null;
        $validdateend = null;
        //判断身份证有效期
        if (!empty($orc_backdata['ValidDate'])) {
            $validdatestart = explode("-", $orc_backdata['ValidDate'])[0];

            $validdateend = explode("-", $orc_backdata['ValidDate'])[1];

            $arr = date_parse_from_format('Y.m.d', $validdateend);//如果是2018年10月1日,那么这里就是 Y年m月d日
            $time = mktime(0, 0, 0, $arr['month'], $arr['day'], $arr['year']);

            if ($time <= time()) {
                $this->error("身份证有效期已失效");
            }
        }

        return array("backdata" => $orc_backdata, "validdatestart" => $validdatestart, "validdateend" => $validdateend);
    }
}
